var app = getApp();
Page({
    data:{
        curPage : "/zofui_sales/cityinfo/info",
        page : {
            isend : false,
            doing : false,
            doo : 'cityinfo',
            pdata : {
                op : 'commentlist',
                page : 1,
                cid : 1,
                isinit : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        color : '',
        info : {},
        puber:{},
        isshowbtn:false,
        iscomment:false,
        content : '',
        isauth : false,
        parent : 0,
        commentidx:0,
        isshowrpt : false,
        reportcontent:'',
        isIPX : app.globalData.isIPX,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        
        self.setData({
            options : options,
            datalist : [],
            'page.pdata.page' : 1,
            'page.isend' : false,
        });
        if( options.scene ){
            var list = options.scene.split('_');
            options.id = list[0] ? list[0] : 0;
            options.zfid = list[1] ? list[1] : 0;
        }

        var zfid = options.zfid ? options.zfid : '';
        var oid = options.oid ? options.oid : '';
        var id = options.id ? options.id : '';

        app.com.getLocation(self,function(loc){
            var postdata = {
                op : 'info',
                zfid : zfid,
                id : id,
                oid : oid,
            };
            app.com.auth(function(user){

                app.com.http('cityinfo','POST',postdata,0,false,'','',function(res){
                    
                    if (!res.data.errno && res.data) {
                        var color = app.com.mainColor(res.data.data.params.style);
                        self.setData({
                            color : color,
                            params : res.data.data.params,
                            info : res.data.data.info,
                            'page.pdata.cid' : res.data.data.info.id,
                            puber : res.data.data.puber,
                            isauth : res.data.data.isauth == 1 ? true : false,
                            sharedata : res.data.data.sharedata,
                            inited : true,
                        },function(){
                            self.onReachBottom();
                        })
                        wx.setNavigationBarTitle({
                            title: res.data.data.info.content ? res.data.data.info.content : '发布'
                        });
                        
                    }else{
                        app.com.alert(res.data.message,function(){
                            app.com.backPage(0);
                        });
                    }
                });
            });
        });
    },
    onUnload(){
        this.data.istop = 1;
    },
    onHide(){
        this.data.istop = 1;
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : '',
        }
    },
    onShareTimeline(){
        return {
            title: this.data.sharedata.title,
            query: this.data.sharedata.query,
            imageUrl : this.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.data.isdown = true;

        this.onLoad( this.data.options );
        this.data.isdown = false;
        wx.stopPullDownRefresh();
    },
    clearsearch(){
        var e = {
            detail : '',
        }
        this.searchfn(e);
    },
    showselect(){
        this.setData({
            showselect : !this.data.showselect
        })
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    showbtn(){
        this.setData({
            isshowbtn : !this.data.isshowbtn
        })
    },
    isub(e){
        var self = this;
        if( wx.requestSubscribeMessage ){
            wx.requestSubscribeMessage({
                tmplIds : self.data.params.submess,
                complete : function(){}
            })
        }
        var p = e.currentTarget.dataset.p ? e.currentTarget.dataset.p : 0;
        self.setData({
            iscomment : !self.data.iscomment,
            commentidx : e.currentTarget.dataset.i,
            parent : p
        })
    },
    onInput(e){
        this.data.content = e.detail.value;
    },
    subcomment(parent){
        var self = this;

        if( self.data.isauth ){
            self.loginauth = self.selectComponent('#loginauth');
            self.loginauth.hideauth();
            return false;
        }

        var postdata = {
            op : 'subcomment',
            cid : self.data.info.id,
            content : self.data.content,
            parent : self.data.parent > 0 ? self.data.parent : 0,
        }
        app.com.confirm('提交后不可修改，确定提交吗？',function(){
            app.com.http('cityinfo','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno && res.data) {
                    
                    app.com.toast(res.data.message,'none');
                    self.setData({
                        content : '',
                        iscomment : false,
                        'info.replynum' : self.data.info.replynum*1 + 1,
                    })
                    if( postdata.parent > 0 ){
                        self.data.datalist[self.data.commentidx].replylist.unshift(res.data.data);
                        self.setData({
                            ['datalist['+self.data.commentidx+']replylist'] : self.data.datalist[self.data.commentidx].replylist
                        })
                    }else{
                        self.data.datalist.unshift(res.data.data);
                        self.setData({
                            datalist : self.data.datalist
                        })
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });
    },
    showallcomment(e){
        var index = e.currentTarget.dataset.i;
        this.setData({
            ['datalist['+index+'].isshowall'] : !this.data.datalist[index].isshowall,
        })
    },
    toCommentList(){
        wx.createSelectorQuery().select("#comment-list").boundingClientRect(function(e) {
            wx.pageScrollTo({
                scrollTop: e.top
            });
        }).exec();
    },
    onPreviewTap(e){
        var self = this;
        var index = e.currentTarget.dataset.index;
        wx.previewImage({
            urls : self.data.info.img,
            current : self.data.info.img[index],
        })
    },
    likeit(e){
        var self = this;
        var index = e.currentTarget.dataset.i;
        var postdata = {
            op : 'likeit',
            id : self.data.info.id,
        }
        app.com.http('cityinfo','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                
                app.com.toast(res.data.message,'none');
                self.setData({
                    'info.islike' : self.data.info.islike == 1 ? 0 : 1,
                    'info.like' : self.data.info.islike == 1 ? self.data.info.like*1 - 1 : self.data.info.like*1 + 1,
                })
                self.onReachBottom();
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    reportcontentfn(e){
        this.setData({
            reportcontent : e.detail.value,
        })
    },
    toReport(e){
        var self = this;
        var id = e.currentTarget.dataset.id;
        self.setData({
            reportid : id,
            reportcontent : '',
            isshowrpt : true,
        })
    },
    subreport(){
        var self = this;
        var postdata = {
            op : 'subreport',
            cid : self.data.info.id,
            content : self.data.reportcontent,
        };
        app.com.http('cityinfo','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                
                app.com.toast(res.data.message,'none');
                self.setData({
                    isshowrpt : false,
                })
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    downorup(e){
        var self = this;
        var type = e.currentTarget.dataset.type;
        var postdata = {
            op : 'downorup',
            cid : self.data.info.id,
            type : type,
        };
        app.com.http('cityinfo','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                
                app.com.toast(res.data.message,'none');
                self.setData({
                    'info.status' : res.data.data.status,
                    isshowbtn : false,
                })
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    onDeleteinfoTap(){
        var self = this;
        var postdata = {
            op : 'delit',
            cid : self.data.info.id,
        };
        app.com.confirm('删除后不能恢复，确定删除吗？',function(){
            app.com.http('cityinfo','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno && res.data) {
                    
                    app.com.toast(res.data.message,'none');
                    app.com.backPage(0);
                }else{
                    app.com.alert(res.data.message);
                }
            });
        })
    },
    paytel(e){
        var self = this;
        var postdata = {
            op : 'paytel',
            cid : self.data.info.id
        };
        app.com.http('cityinfo','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                
                wx.requestPayment({
                    appId : res.data.data.appId,
                    timeStamp: res.data.data.timeStamp,
                    nonceStr: res.data.data.nonceStr,
                    package: res.data.data.package,
                    signType: 'MD5',
                    paySign: res.data.data.paySign,
                    success: function (payres) {
                        if( payres.errMsg == 'requestPayment:ok' ){
                            self.seetel();
                        }
                    },
                    fail: function (payres) {}
                });
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    seetel(){
        var self = this;
        if(this.data.istop == 1) return false;
        app.com.http('cityinfo','POST',{cid:self.data.info.id,op:'seetel'},0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                self.setData({
                    ['info.tel'] : res.data.data.tel,
                    ['info.iscantel'] : 1,
                })
                app.com.toast('已支付成功，可点击按钮拨打电话','none');
            }else{
                setTimeout(function(){
                    self.seetel();
                },1000)
            }
        })
    },
    shareit(){
        this.selectComponent('#share').onClose();
    },
    authed(){
        this.setData({
            isauth : false,
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },

})